﻿//   AppStorageServiceProxy.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA
namespace CKSAS.AppStorage.SharedService
{
    using System;
    using Microsoft.SharePoint.Administration;
    using System.Runtime.InteropServices;

    /// <summary>
    /// 
    /// </summary>
    [Guid("f10a258f-7d12-4ce0-8e36-a05bd39e3bbe")]
    [SupportedServiceApplication("8176b564-7770-46d0-a8ca-584221acc325", "1.0.0.0",
        typeof (AppStorageServiceApplicationProxy))]
    internal sealed class AppStorageServiceProxy : SPIisWebServiceProxy, IServiceProxyAdministration
    {
        /// <summary>
        /// 
        /// </summary>
        private const string SERVICE_PROXY_TYPE_NAME = "Application Storage Service Proxy";

        /// <summary>
        /// 
        /// </summary>
        private const string SERVICE_PROXY_TYPE_DESCRIPTION =
            "Connects to the Application Storage service instances running within the farm";

        /// <summary>
        /// Initializes a new instance of the <see cref="AppStorageServiceProxy"/> class.
        /// </summary>
        public AppStorageServiceProxy()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AppStorageServiceProxy"/> class.
        /// </summary>
        /// <param name="farm"></param>
        public AppStorageServiceProxy(SPFarm farm)
            : base(farm)
        {
        }

        /// <summary>
        /// </summary>
        /// <param name="serviceApplicationProxyType"></param>
        /// <param name="name"></param>
        /// <param name="serviceApplicationUri"></param>
        /// <param name="provisioningContext"></param>
        /// <returns>
        /// Returns <see cref="T:Microsoft.SharePoint.Administration.SPServiceApplicationProxy"/>.
        /// </returns>
        SPServiceApplicationProxy IServiceProxyAdministration.CreateProxy(Type serviceApplicationProxyType, string name,
                                                                          Uri serviceApplicationUri,
                                                                          SPServiceProvisioningContext
                                                                              provisioningContext)
        {
            if (serviceApplicationProxyType != typeof (AppStorageServiceApplicationProxy))
            {
                throw new NotSupportedException();
            }

            return new AppStorageServiceApplicationProxy(name, this, serviceApplicationUri);
        }

        /// <summary>
        /// </summary>
        /// <param name="serviceApplicationProxyType"></param>
        /// <returns>
        /// Returns <see cref="T:Microsoft.SharePoint.Administration.SPAdministrationLink"/>.
        /// </returns>
        SPAdministrationLink IServiceProxyAdministration.GetCreateProxyLink(Type serviceApplicationProxyType)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// </summary>
        /// <param name="serviceApplicationProxyType"></param>
        /// <returns>
        /// Returns <see cref="T:Microsoft.SharePoint.Administration.SPPersistedTypeDescription"/>.
        /// </returns>
        SPPersistedTypeDescription IServiceProxyAdministration.GetProxyTypeDescription(Type serviceApplicationProxyType)
        {
            if (serviceApplicationProxyType != typeof (AppStorageServiceApplicationProxy))
            {
                throw new NotSupportedException();
            }

            return new SPPersistedTypeDescription(SERVICE_PROXY_TYPE_NAME, SERVICE_PROXY_TYPE_DESCRIPTION);
        }

        /// <summary>
        /// </summary>
        /// <returns>
        /// Returns <see cref="T:System.Type"/>.
        /// </returns>
        Type[] IServiceProxyAdministration.GetProxyTypes()
        {
            return new Type[] {typeof (AppStorageServiceApplicationProxy)};
        }

        /// <summary>
        /// Gets the application proxy given by name
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        internal AppStorageServiceApplicationProxy GetApplicationProxy(string name)
        {
            return this.ApplicationProxies.GetValue<AppStorageServiceApplicationProxy>(name);
        }

        /// <summary>
        /// Gets the local instance of a service proxy.
        /// </summary>
        internal static AppStorageServiceProxy Local
        {
            get { return SPFarm.Local.ServiceProxies.GetValue<AppStorageServiceProxy>(); }
        }
    }
}